package com.wjm.blog.common.base.response;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.wjm.blog.common.enums.ResponseEnum;
import com.wjm.blog.common.util.MessageUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @Description: 返回结果公共类
 * @Author: Ailos
 * @Date: 2020/4/20 11:28
 * @Version: 1.0
 */
@ApiModel
public class Result<T> {

    @ApiModelProperty(value = "返回编码，200：成功，其他值：失败", required = true)
    private String code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回结果")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    @JSONField(serialize = false)
    @JsonIgnore
    public boolean isSuccess() {
        return ResponseEnum.SUCCESS.getCode().equalsIgnoreCase(code);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T result) {
        this.data = result;
    }

    public Result() {
        this.code = ResponseEnum.SUCCESS.getCode();
    }

    public Result(ResponseEnum response) {
        this.code = response.getCode();
        this.message = MessageUtil.getMessage(response.getMessage());
    }

    public static Result success(Object result) {
        Result response = success();
        response.setData(result);
        return response;
    }

    public static Result success() {
        return new Result(ResponseEnum.SUCCESS);
    }

    public static Result error(ResponseEnum responseEnum) {
        return new Result(responseEnum);
    }

    public static Result error(String message) {
        return error(ResponseEnum.ERROR.getCode(), message, null);
    }

    public static Result error(String code, String message) {
        return error(code, message, null);
    }

    public static Result error(ResponseEnum responseEnum, Object... msgArgs) {
        return error(responseEnum.getCode(), responseEnum.getMessage(), msgArgs);
    }

    /**
     * BizException 业务异常, 构建返回几个
     *
     * @param code    错误编码
     * @param message BizException错误消息定义KEY
     * @param msgArgs 异常消息错误可变参数
     * @return Result
     */
    public static Result error(String code, String message, Object... msgArgs) {
        Result response = new Result();
        response.setCode(code);
        if (msgArgs != null && msgArgs.length > 0) {
            response.setMessage(String.format(MessageUtil.getMessage(message), msgArgs));
        } else {
            response.setMessage(MessageUtil.getMessage(message));
        }
        return response;
    }

    public static Result error() {
        return new Result(ResponseEnum.ERROR);
    }

    public static Result errorResult(Object result) {
        Result response = error();
        response.setData(result);
        return response;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}